import { fs } from '@tauri-apps/api';
import { open } from '@tauri-apps/api/dialog';
import { basename, extname, join } from '@tauri-apps/api/path';
import { open as shellOpen } from '@tauri-apps/api/shell';

import { v4 as uuidv4 } from 'uuid';
import { convertFileSrc } from '@tauri-apps/api/tauri';
import type { UploadFile } from 'antd';
import config from '../config';

export const generateTmpPathOrFile = async (target_file: string) => {
  const {
    path: {
      tmp,
    },
  } = await config();
  return await join(tmp, target_file);
};

export const tmpPath = async () => {
  const {
    path: {
      tmp,
    },
  } = await config();

  return tmp;
};

export const dataPath = async () => {
  const {
    path: {
      data,
    },
  } = await config();

  return data;
};

export const downloadPath = async () => {
  const {
    path: {
      download,
    },
  } = await config();

  return download;
};

export const tplPath = async () => {
  const {
    path: {
      tpl,
    },
  } = await config();

  return tpl;
};

export const filesPath = async () => {
  const {
    path: {
      files,
    },
  } = await config();

  return files;
};

export const appPath = async () => {
  const {
    path: {
      app,
    },
  } = await config();

  return app;
};

export const resourcePath = async (path: string) => {
  return await convertFileSrc(await join(await appPath(), path), 'fill');
};

export interface IUploadFile extends UploadFile {
  base: string
  ext: string
}

export const copyFile = async (path: string): Promise<IUploadFile> => {
  const name = await basename(path);
  const ext = await extname(path);

  const uuid = uuidv4();
  const originName = `${name}`;
  const uniqueName = `${uuid}.${ext}`;
  const target = await join(await filesPath(), uniqueName);
  await fs.copyFile(path, target);

  const url = target.replace(await appPath(), '');

  return {
    uid: uuid, // 文件id
    name: originName, // 原名
    fileName: uniqueName, // 文件名
    base: url, // 相对路径
    size: 0,
    ext,
    url: await resourcePath(url), // 资源全路径
  };
};

export const handleSelectFile = async (extensions = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'jpg', 'png', 'jpeg', 'txt', 'chrtc', 'et']) => {
  const selected = await open({
    multiple: true,
    filters: [{
      name: '文件',
      extensions,
    }],
  });

  if (Array.isArray(selected)) {
    console.log(selected);
    const lists = await Promise.all(selected.map(async (item) => {
      return await copyFile(item);
    }));

    return Promise.resolve(lists);
    // user selected multiple directories
  }
  else if (selected === null) {
    // user cancelled the selection
    return Promise.resolve([]);
  }
  else {
    return Promise.resolve([]);
  }
};

export const getFileName = (path: string) => {
  const position = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));

  return position < 0 ? path : path.substring(position + 1);
};

export const openFile = async (file: string) => {
  await shellOpen(file);
};
